/**
 * author:wos
 * time:2024-1-4
 * title:导出日志
 */
const {makeSqlSelect} = require('../../../../utils/makeSql'),
    parseQueryParams = require("../../../../utils/parseQueryParams"),
    xlsx = require('node-xlsx')

async function list(ctx, next) {

    try {
        let rs = await global.query(`select * from sys_log_system order by id desc`)
        let data = []
        for (let i in rs){
            data.push([
                rs[i].id,
                rs[i].creatDatetime,
                rs[i].url,
                rs[i].postQuery,
                rs[i].getQuery,
                rs[i].title,
                rs[i].method,
                rs[i].userIp,
                rs[i].token,
                rs[i].username,
                rs[i].userAgent,
                rs[i].code,
                rs[i].msg,
                rs[i].runTime,
            ])
        }

        const excelData = [
            ['编号','访问时间','访问地址','POST,PU请求','GET DELETE请求','类型','方法','IP','TOKEN','用户','浏览器','状态码','返回信息','运行时间'],
            ...data
        ]
        const buffer = xlsx.build([{name: 'log', data: excelData}]);
        ctx.status = 200
        ctx.set('Content-Type', 'application/vnd.openxmlformats')
        // 设置文件名信息请求头
        ctx.set('Content-Disposition', "attachment; filename=" + encodeURIComponent("导出的日志") + ".xlsx")
        // 文件名信息由后端返回时必须设置该请求头,否则前端拿不到Content-Disposition响应头信息
        ctx.set("Access-Control-Expose-Headers", "Content-Disposition")
        // 将buffer返回给前端
        ctx.body = buffer
    } catch (err) {
        console.error(err)
        global.serverError(ctx)
    }
}
module.exports = list


